<template>
  <md-nav-bar title="我的钱包" backgroundColor="#ffffff" />
  <view class="income-container">
    <!-- 顶部卡片 -->
    <view class="card">
      <view class="card-info">
        <view class="card-title">金额</view>
        <view class="card-amount">￥{{ formatUnitCentToYuan(data.info?.frozenPrice, 2) }}</view>
        <view class="card-available">可提现 ￥{{ formatUnitCentToYuan(data.info?.brokeragePrice, 2) }}</view>
      </view>
      <button class="withdraw-btn" @click="onWithdraw">提现</button>
    </view>

    <!-- tab切换 -->
    <view class="tab-bar">
      <view class="tab-item" :class="{ active: tabIndex === 0 }" @click="tabIndex = 0">收入</view>
      <view class="tab-item" :class="{ active: tabIndex === 1 }" @click="tabIndex = 1">提现</view>
    </view>

    <!-- 收入明细 -->
    <view v-if="tabIndex === 0" class="list">
      <md-page-list :api="api.Pay.profitPage" :params="{ memberId: data.memberId }" @change="pageChange">
        <view v-for="(item, idx) in data.list" :key="idx" class="list-item">
          <view class="item-main">
            <view class="item-title">{{ item.title }}</view>
            <view class="item-amount">+{{ formatUnitCentToYuan(item.price, 2) }}</view>
          </view>
          <view class="item-desc">{{ '' }}</view>
          <view class="item-date">{{ item.time }}</view>
        </view>
      </md-page-list>
    </view>
    <view v-if="tabIndex === 1" class="list">
      <md-page-list :api="api.Pay.withdrawalPage" :params="{ memberId: data.memberId }" @change="pageWithdrawalChange">
        <view v-for="(item, idx) in data.withdrawalList" :key="idx" class="list-item">
          <view class="item-main">
            <view class="item-title">{{ item.statusName }}</view>
            <view class="item-amount is-out">-{{ formatUnitCentToYuan(item.price, 2) }}</view>
          </view>
          <view class="item-desc">{{ item.auditReason }}</view>
          <view class="item-date">{{ item.createTime }}</view>
        </view>
      </md-page-list>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import {onLoad, onShow} from '@dcloudio/uni-app';
// 接口
import api from '@/api';
import type { Pay } from '@/api/data';
import { formatUnitCentToYuan } from '@/utils/util';

const data = reactive({
  memberId: -1,
  info: {} as Pay.BrokeragePrice.Data,
  list: [] as Pay.ProfitPage.Data['list'],
  withdrawalList: [] as Pay.WithdrawalPage.Data['list'],
});

const tabIndex = ref(0);

const pageChange = (list: Pay.ProfitPage.Data['list']) => {
  data.list = list;
};

const pageWithdrawalChange = (list: Pay.WithdrawalPage.Data['list']) => {
  data.withdrawalList = list;
};

function onWithdraw() {
  uni.navigateTo({
    url: `/pages/sub/my/withdraw`,
  });
}

/**
 * 接口相关
 */
const fetchGetBrokeragePrice = async () => {
  try {
    const res = await api.Pay.brokeragePrice();
    data.info = res.data;
  } catch (e) {}
};

onLoad(() => {
  data.memberId = uni.getStorageSync('auth')?.userId;
});

onShow(()=>{
  fetchGetBrokeragePrice();
})
</script>

<style lang="scss" scoped>
.income-container {
  min-height: 100vh;
  background: #f5f8f7;
  padding-bottom: 32rpx;
}
.card {
  margin: 32rpx 24rpx 0 24rpx;
  background: linear-gradient(135deg, #2ca88a 0%, #0b5e4a 100%);
  border-radius: 40rpx;
  padding: 40rpx 36rpx 36rpx 36rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
}
.card-info {
  display: flex;
  flex-direction: column;
  color: #fff;
}
.card-title {
  font-size: 28rpx;
  opacity: 0.9;
  margin-bottom: 8rpx;
}
.card-amount {
  font-size: 56rpx;
  font-weight: bold;
  margin-bottom: 8rpx;
  letter-spacing: 2rpx;
}
.card-available {
  font-size: 28rpx;
  opacity: 0.9;
}
.withdraw-btn {
  position: absolute;
  right: 36rpx;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: 2rpx solid #fff;
  color: #fff;
  font-size: 32rpx;
  border-radius: 40rpx;
  padding: 0 40rpx;
  height: 64rpx;
  line-height: 64rpx;
  font-weight: 500;
  box-shadow: none;
}
.tab-bar {
  display: flex;
  margin: 40rpx 0 0 0;
  background: transparent;
  border-bottom: 2rpx solid #e5e5e5;
  margin-left: 24rpx;
  margin-right: 24rpx;
}
.tab-item {
  flex: 1;
  text-align: center;
  font-size: 32rpx;
  color: #222;
  padding-bottom: 16rpx;
  position: relative;
  font-weight: 500;
  opacity: 0.8;
}
.tab-item.active {
  color: #1fa37a;
  opacity: 1;
}
.tab-item.active::after {
  content: '';
  display: block;
  width: 48rpx;
  height: 6rpx;
  background: #1fa37a;
  border-radius: 3rpx;
  position: absolute;
  left: 50%;
  bottom: -2rpx;
  transform: translateX(-50%);
}
.list {
  margin: 0 24rpx;
  margin-top: 24rpx;
}
.list-item {
  background: #fff;
  border-radius: 24rpx;
  margin-bottom: 24rpx;
  padding: 32rpx 28rpx 24rpx 28rpx;
  box-shadow: 0 2rpx 12rpx 0 #e0e0e0;
}
.item-main {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.item-title {
  font-size: 30rpx;
  color: #222;
  font-weight: 500;
  flex: 1;
  margin-right: 16rpx;
}
.item-amount {
  font-size: 32rpx;
  color: #01604a;
  font-weight: 600;
  min-width: 100rpx;
  text-align: right;
}
.item-desc {
  font-size: 26rpx;
  color: #888;
  margin-top: 8rpx;
}
.item-date {
  font-size: 24rpx;
  color: #bbb;
  margin-top: 8rpx;
}
.empty {
  text-align: center;
  color: #bbb;
  font-size: 28rpx;
  margin-top: 80rpx;
}
</style>
